package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.R;
import com.reggie.common.ReggieConstants;
import com.reggie.dto.UserDto;
import com.reggie.entity.User;
import com.reggie.service.UserService;
import com.reggie.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        if (StringUtils.isNotBlank(user.getPhone())) {
            String s = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}", s);
            ValueOperations valueOperations = redisTemplate.opsForValue();
            valueOperations.set(ReggieConstants.USER_CODE_REDIS_PREFIX+user.getPhone(),s,1, TimeUnit.MINUTES);
           // request.getSession().setAttribute(user.getPhone(), s);
        }
        return R.success("短信发送成功");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody UserDto userDto, HttpServletRequest request) {
        String phone = userDto.getPhone();
        String code = userDto.getCode();
        if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(phone)) {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            String attribute = (String) valueOperations.get(ReggieConstants.USER_CODE_REDIS_PREFIX+phone);
            //String attribute = (String) request.getSession().getAttribute(phone);
            if (code.equals(attribute)) {
                //request.getSession().removeAttribute(phone);
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getPhone, phone);
                User user = userService.getOne(userLambdaQueryWrapper);
                if (user == null) {
                    user = new User();
                    user.setPhone(phone);
                    user.setStatus(1);
                    user.setName(phone);
                    userService.save(user);
                }
                if (user.getStatus() == 0)
                    return R.error("用户已被禁用");
                redisTemplate.delete(ReggieConstants.USER_CODE_REDIS_PREFIX+phone);
                request.getSession().setAttribute(ReggieConstants.USER_SESSION_KEY, Long.parseLong(phone));
                return R.success(user);
            }
        }
        return R.error("验证码错误");
    }

    @PostMapping("/loginout")
    public R<String> loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute(ReggieConstants.USER_SESSION_KEY);
        return R.success("登出成功");
    }
}
